package com.aiguigu;

import java.util.concurrent.*;

/*juc小口诀第一式
 1 在高内聚低耦合前提下，    线程      操作      资源类
 */
public class SaleTicketDemo {
    public static void main(String[] args) {
//        MyTicket t= new MyTicket();
//        Thread t1 = new Thread(t,"赵四");
//        Thread t2 = new Thread(t,"广坤");
//        Thread t3 = new Thread(t,"刘能");
//
//        t1.start();
//        t2.start();
//        t3.start();

//        v1===================================
        /*Ticket ticket = new Ticket();
        new Thread(new Runnable() {
         @Override
         public void run() {
             for (int i = 1; i <= 50; i++) {
                 ticket.sale();
             }
         }
     }, "t1").start();
     new Thread(new Runnable() {
         @Override
         public void run() {
             for (int i = 1; i <= 50; i++) {
                 ticket.sale();
             }

         }
     }, "t3").start();
     new Thread(new Runnable() {
         @Override
         public void run() {
             for (int i = 1; i <= 50; i++) {
                 ticket.sale();
             }
         }
     }, "t3").start();*/

        Ticket ticket = new Ticket();

//        new Thread(() -> {for (int i = 1; i <= 50; i++) ticket.sale();}, "t1").start();
//        new Thread(() -> {for (int i = 1; i <= 50; i++) ticket.sale();}, "t2").start();
//        new Thread(() -> {for (int i = 1; i <= 50; i++) ticket.sale();}, "t3").start();

        ExecutorService threadPool=new ThreadPoolExecutor(
                3,
                3,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(50),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
//        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        try
        {
            for (int i = 1; i <= 50; i++) {
                threadPool.execute(()->{
                    ticket.sale();
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }


    }

}
